//
//  WBComposeToolBar.swift
//  0829-Weibo
//
//  Created by 毕仲阳 on 16/9/8.
//  Copyright © 2016年 mycompany. All rights reserved.
//

import UIKit
/**
 用于判断按钮类型的枚举
 */
enum ComposeToolBarButtonType:Int {
    case picture = 0, mention, trend, emotion, add
}
/**
 定义协议
 */
protocol WBComposeToolBarDelegate:NSObjectProtocol {
    func composeTollBar(toolBar:WBComposeToolBar,buttonType:ComposeToolBarButtonType)
}
class WBComposeToolBar: UIView {
    ///代理属性
    weak var delegate : WBComposeToolBarDelegate?
    //
    var isSystemKeyboard: Bool = true {
        didSet{
            let button = self.viewWithTag(3) as! UIButton
            var imgName = "compose_keyboardbutton_background"
            if isSystemKeyboard {
                imgName = "compose_emoticonbutton_background"
            }
            button.setImage(UIImage(named:imgName), for: UIControlState.normal)
            button.setImage(UIImage(named:"\(imgName)_highlighted"), for: UIControlState.highlighted)
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.setUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private func setUI()
    {
        self.backgroundColor = UIColor(patternImage: UIImage(named: "compose_toolbar_background")!)
        self.addSubview(stackView)
        stackView.snp_makeConstraints { (make)->Void in
            make.edges.equalTo(self)
        }
        addChildButton(imgNamed: "compose_toolbar_picture",type:.picture)
        addChildButton(imgNamed: "compose_mentionbutton_background",type:.mention)
        addChildButton(imgNamed: "compose_trendbutton_background",type:.trend)
        addChildButton(imgNamed: "compose_emoticonbutton_background",type:.emotion)
        addChildButton(imgNamed: "compose_add_background",type:.add)

    }

    private func addChildButton(imgNamed:String,type:ComposeToolBarButtonType){
        let button = UIButton()
        button.tag = type.rawValue
        button.addTarget(self, action: #selector(buttonTouch(button:)), for: UIControlEvents.touchUpInside)
        button.setImage(UIImage(named:imgNamed), for: UIControlState.normal)
        button.setImage(UIImage(named:"\(imgNamed)_highlighted"), for: UIControlState.highlighted)
        stackView.addArrangedSubview(button)
    
    }
    @objc private func buttonTouch(button:UIButton){
        let type = ComposeToolBarButtonType(rawValue: button.tag)
        self.delegate?.composeTollBar(toolBar: self, buttonType: type!)
    }
    lazy var stackView:UIStackView = {
        let view = UIStackView()
        view.distribution = .fillEqually
        return view
    }()
}
